package com.crm.controller;

import com.crm.entity.CustomerService;
import com.crm.entity.PageBean;
import com.crm.service.CustomerServiceService;
import com.crm.util.ResponseUtil;
import com.crm.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客服服务Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/customerService")
public class CustomerServiceController {

    @Resource
    private CustomerServiceService customerServiceService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    /**
     * 分页条件查询客户服务
     *
     * @param page
     * @param rows
     * @param createTimeFrom
     * @param createTimeTo
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows,
                       String createTimeFrom, String createTimeTo, CustomerService s_customerService, HttpServletResponse response) throws Exception {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        if (s_customerService.getCustomer() != null) {
            map.put("name", StringUtil.formatLike(s_customerService.getCustomer().getName()));
        }
        map.put("overview", StringUtil.formatLike(s_customerService.getOverview()));
        map.put("serveType", s_customerService.getServeType());
        map.put("state", s_customerService.getState());
        map.put("createTimeFrom", createTimeFrom);
        map.put("createTimeTo", createTimeTo);
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<CustomerService> customerServiceList = customerServiceService.find(map);
        Long total = customerServiceService.getTotal(map);

        JSONObject result = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(customerServiceList, jsonConfig);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("customer.name",((JSONObject)jsonObject.get("customer")).get("name"));
        }
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }


    /**
     * 添加或者修改客服服务
     *
     * @param customerService
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(CustomerService customerService, HttpServletResponse response) throws Exception {
        if (customerService.getId() == null) {
            customerServiceService.add(customerService);
        } else {
            customerServiceService.update(customerService);
        }
        JSONObject result = new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

}
